<?php
class CiusubjectsController extends AppController {

	var $name = 'Ciusubjects';
        var $components = array('Session');
        var $uses=array('Ciusubject','Ciuplan');
	function index() {
		$this->Ciusubject->recursive = 0;
		$this->set('ciusubjects', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ciusubject', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ciusubject', $this->Ciusubject->read(null, $id));
	}

	function add($ciuplan=null , $ciulanguage=null) {
                if (!empty($this->data)) {
			$this->Ciusubject->create();
                        if ($this->Ciusubject->save($this->data)) {
				$this->Session->setFlash(__('The ciusubject has been saved', true));
                                $this->redirect(array('controller' => 'ciuplans', 'action' => 'index'));
			// corregir	$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $ciuplan));// se redirecciona al controlador ciuplans, funcion view 
                                
			} else {
				$this->Session->setFlash(__('The ciusubject could not be saved. Please, try again.', true));
			}
		}
		$ciulanguages = $this->Ciusubject->Ciulanguage->find('list',array('conditions'=>array('Ciulanguage.id'=> $ciulanguage ) ));
                $prerequisites = $this->Ciusubject->find('list',array('conditions'=>array('Ciusubject.ciuplan_id'=>array($ciuplan,5)  ) )); //5 Es el id del plan "Ninguno".
		$ciuplans = $this->Ciusubject->Ciuplan->find('list',array('conditions'=>array('Ciuplan.id'=>$ciuplan )));
		$this->set(compact('ciulanguages', 'ciuplans','prerequisites'));
	}

	function edit($id=null, $ciuplan=null , $ciulanguage=null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ciusubject', true));
			$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $ciuplan));
		}
		if (!empty($this->data)) {
			if ($this->Ciusubject->save($this->data)) {
				$this->Session->setFlash(__('The ciusubject has been saved', true));
				$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $this->data["Ciusubject"]["Ciuplan"]));
                                
			} else {
				$this->Session->setFlash(__('The ciusubject could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ciusubject->read(null, $id);
		}
		$ciulanguages = $this->Ciusubject->Ciulanguage->find('list',array('conditions'=>array('Ciulanguage.id'=> $ciulanguage ) ));
                $prerequisites = $this->Ciusubject->find('list',array('conditions'=>array('Ciusubject.ciuplan_id'=>array($ciuplan,5)  ) )); //5 Es el id del plan "Ninguno".
		$ciuplans = $this->Ciusubject->Ciuplan->find('list',array('conditions'=>array('Ciuplan.id'=>$ciuplan )));
		$this->set(compact('ciulanguages', 'ciuplans','prerequisites'));
	}

	function delete($id = null, $plan=null) { // Se recibe el plan, para direccionarlo a la vista view del controlador ciuplan
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ciusubject', true));
			$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $plan));
		}
		if ($this->Ciusubject->delete($id)) {
			$this->Session->setFlash(__('Ciusubject deleted', true));
			$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $plan));
		}
                
		$this->Session->setFlash(__('Ciusubject was not deleted', true));
		$this->redirect(array('controller' => 'ciuplans', 'action' => 'view', $plan));
	}
}